﻿#region references
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

/* Project dependencies */
using IcaGames.Logging;
using IcaGames.Objects;
#endregion

namespace IcaGames.Database.ObjectManagement
{
    /*
     * Class that holds common methods used to manage Game objects.
     * 
     * @author
     * Rick Oonk
     * 
     * @version
     * 1.0
     * 
     * @update
     * 20-11-2009 by Rick Oonk.
     * Created Class and added attributes and standard functions. 
     */ 
    public static class GameManagement
    {
        /*
         * Function to get a list with favorite Games from the database.
         * 
         * @param
         * gameNr: The number of the Game that can't be included in the list.
         * games: The list with Games that has to be loaded.
         * 
         * @returns
         * True if the list was succesfully loaded.
         * False if the list was not succesfully loaded.
         */ 
        public static bool GetFavoriteGames(List<Game> games, int gameNr)
        {
            SqlCommand command = null;
            SqlDataReader dr = null;
            string commandText = "spGetFavoriteGames";
            try
            {
                command = Connection.GetCommand(commandText);
                command.Parameters.AddWithValue("@gameNr", gameNr);
                command.CommandType = CommandType.StoredProcedure;

                dr = Connection.GetSqlDataReader(command);
                while (dr.Read())
                {
                    Game game = new Game
                    {
                        GameNr = Cast.CastToInteger(dr["GAME_NR"]),
                        Title = Cast.CastToString(dr["TITEL"]),
                        Price = Cast.CastToDecimal(dr["PRIJS"]),
                        Sold = Cast.CastToInteger(dr["VERKOCHT"]),
                        Publisher = new Publisher(
                            Cast.CastToInteger(dr["MAKER_ID"]), 
                            Cast.CastToString(dr["NAAM"])),
                        Platform = (Platform)Cast.CastToInteger(dr["PLATFORM_ID"]),
                    };
                    games.Add(game);
                }
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while getting Favorite Games", "GAMEMAMANGEMENT");
                Log.CreateLog(ex.Message, "GAMEMAMANGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command, dr);
            }

            return true;
        }

        /*
         * Function to get a list with Games with a specified Publisher.
         * 
         * @param
         * publisherId: The id of the Publisher.
         * games: The list that has to be loaded with Games.
         * 
         * @returns
         * True if the list has succesfully been loaded.
         * False if the list has not succesfully been loaded.
         */ 
        public static bool GetPublisherGames(int publisherId, List<Game> games)
        {
            SqlCommand command = null;
            SqlDataReader dr = null;
            string commandText = "spGetPublisherGames";

            try
            {
                command = Connection.GetCommand(commandText);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@publisherId", publisherId);

                dr = Connection.GetSqlDataReader(command);
                while (dr.Read())
                {
                    Game game = new Game();
                    game.GameNr = Cast.CastToInteger(dr["GAME_NR"]);
                    game.Title = Cast.CastToString(dr["TITEL"]);
                    game.Price = Cast.CastToDecimal(dr["PRIJS"]);
                    game.GameSize = Cast.CastToInteger(dr["GROOTTE"]);
                    game.DescriptionShort = Cast.CastToString(dr["BESCHRIJVING_KORT"]);
                    game.DescriptionLong = Cast.CastToString(dr["BESCHRIJVING_UITGEBREID"]);
                    game.Stock = Cast.CastToInteger(dr["VOORRAAD"]);
                    game.Publisher = new Publisher(
                        Cast.CastToInteger(dr["MAKER_ID"]),
                        Cast.CastToString(dr["NAAM"]));
                    game.Platform = (Platform)Cast.CastToInteger(dr["PLATFORM_ID"]);
                    games.Add(game);
                }
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while getting Publisher Games", "GAMEMAMANGEMENT");
                Log.CreateLog(ex.Message, "GAMEMAMANGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command, dr);
            }

            return true;
        }

        /*
         * Function to load a Game from the database.
         * 
         * @param
         * gameNr: The gameNr of the specified Game.
         * game: The Game Object that has to be loaded.
         * platform: The Platform for the specified Game.
         * 
         * @returns
         * True if the Game was succesfully loaded from the database.
         * False if the Game was not succesfully loaded from the database.
         */ 
        public static bool GetGame(int gameNr, Game game, string platform)
        {
            SqlCommand command = null;
            SqlDataReader dr = null;
            string commandText = "spGetGame";
            try
            {
                int platformId = PlatformValue.GetPlatformValue(platform);
                command = Connection.GetCommand(commandText);
                command.Parameters.AddWithValue("@gameNr", gameNr);
                command.Parameters.AddWithValue("@platformId", platformId);
                command.CommandType = CommandType.StoredProcedure;

                dr = Connection.GetSqlDataReader(command);
                while (dr.Read())
                {
                    game.GameNr = Cast.CastToInteger(dr["GAME_NR"]);
                    game.Title = Cast.CastToString(dr["TITEL"]);
                    game.Price = Cast.CastToDecimal(dr["PRIJS"]);
                    game.GameSize = Cast.CastToInteger(dr["GROOTTE"]);
                    game.DescriptionShort = Cast.CastToString(dr["BESCHRIJVING_KORT"]);
                    game.DescriptionLong = Cast.CastToString(dr["BESCHRIJVING_UITGEBREID"]);
                    game.Stock = Cast.CastToInteger(dr["VOORRAAD"]);
                    game.Publisher = new Publisher(
                        Cast.CastToInteger(dr["MAKER_ID"]),
                        Cast.CastToString(dr["NAAM"]));
                    game.Platform = (Platform)Cast.CastToInteger(dr["PLATFORM_ID"]);
                    game.Sold = Cast.CastToInteger(dr["VERKOCHT"]);
                }
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while getting Game", "GAMEMAMANGEMENT");
                Log.CreateLog(ex.Message, "GAMEMAMANGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command);
            }

            return true;
        }

        /*
         * Function to get the stock for a specified Game of a specified Platform.
         * 
         * @param
         * gameNr: The gameNr of the specified Game.
         * platformId: The Platform for the specified Game.
         * 
         * @returns
         * The stock for the specified Game.
         */ 
        public static int GetGameStock(int gameNr, int platformId)
        {
            SqlCommand command = null;
            string commandText = "spGetGameStock";
            int stock = 0;
            try
            {
                command = Connection.GetCommand(commandText);
                command.Parameters.AddWithValue("@gameNr", gameNr);
                command.Parameters.AddWithValue("@platformId", platformId);
                command.CommandType = CommandType.StoredProcedure;
                stock = Convert.ToInt32(command.ExecuteScalar());
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while getting Game stock", "GAMEMAMANGEMENT");
                Log.CreateLog(ex.Message, "GAMEMAMANGEMENT");
                stock = -1;
            }
            finally
            {
                Connection.CloseCommand(command);
            }

            return stock;
        }
    }
}
